<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\user\Application as Model;

class Application extends Controller
{
    public function list()
    {
        $model = new Model;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 新增
     * @return array
     */
    public function add()
    {
        $model = new Model;
        // 更新记录
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('报名成功');
        }
        return $this->renderError($model->getError() ?: '报名失败');
    }

    /**
     * 编辑
     * @return array
     */
    public function edit($application_id)
    {
        $model = Model::detail($application_id);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    public function detail($application_id)
    {
        $detail = Model::detail($application_id, ['user']);
        return $this->renderSuccess(compact('detail'));
    }
}
